<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge :dot="!!messageUnreadCount">
        <Avatar :src="userAvatar"/>
      </Badge>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <DropdownItem name="userNameShow">{{userName}}</DropdownItem>
        <DropdownItem name="message" style="display: none;">
          消息中心<Badge style="margin-left: 10px" :count="messageUnreadCount"></Badge>
        </DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
export default {
  'name': 'User',
  'props': {
  
  },
  'computed' :{
      userName() {
          return this.$store.state.user.userName;
      },
      userAvatar() {
          return this.$store.state.user.avatorImgPath
      },
      messageUnreadCount() {
          return this.$store.state.user.unreadCount
      },
  },
  'methods': {
    ...mapActions('user',[
      'handleLogOut'
    ]),
    logout() {
      this.handleLogOut().then(() => {
        this.$router.push({
          'name': 'login'
        });
        location.href += '';//退出后刷新，使得新的登录用户可以重新获取导航权限，配合rbac使用
      })
    },
    message() {
      this.$router.push({
        'name': 'message_page'
      })
    },
    handleClick(name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'message': this.message()
          break
      }
    }
  }
}
</script>
